package edu.dayu.config;

import edu.dayu.exception.YyghException;
import edu.dayu.helper.HttpRequestHelper;
import edu.dayu.result.ResultCodeEnum;
import edu.dayu.service.HospitalSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

////@Configuration
//public class ApiIntercept implements WebMvcConfigurer {
//    @Autowired
//    private HospitalSetService hospitalSetService;
//
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new HandlerInterceptor() {
//            @Override
//            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//                Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//                //必须参数校验
//                String hoscode = (String) paramMap.get("hoscode");
//                if(StringUtils.isEmpty(hoscode)) {
//                    throw new YyghException(ResultCodeEnum.PARAM_ERROR);
//                }
//                //签名校验
//                if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//                    throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//                }
//                return true;
//            }
//        }).addPathPatterns("/api/hosp");
//    }
//}
